﻿using System.Collections.Generic;
using System.Linq;

namespace nRoute.Components.Composition.Adapters
{
    public class EnumerableLazyMetaLocatorAdapter<T, TMetadata>
        : LocatorAdapterBase<T, IEnumerable<Lazy<T, TMetadata>>>
        where 
            T : class
        where    
            TMetadata : class
    {
        public EnumerableLazyMetaLocatorAdapter()
            : base((b, s) =>
            {
                // note_ we are ignoring the string "s" type sent in - and use of as TMetadata
                if (b.ResourceCatalog.IsResourceable<T>())
                {
                    return ((IResourceCatalogEx)b.ResourceCatalog).GetResourceCatalog<T>().Select(
                        (l) => new Lazy<T, TMetadata>(() => (T)l.GetResource(b), l as TMetadata)).Where((r) => r.Metadata != null);
                }
                else
                {
                    return Enumerable.Empty<Lazy<T, TMetadata>>();
                }
            }) { }
    }
}